package com.xinrui.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xinrui.sunnyday.constant.MessageConstant;
import com.xinrui.sunnyday.entity.Result;
import com.xinrui.sunnyday.pojo.Menu;
import com.xinrui.sunnyday.pojo.Role;
import com.xinrui.sunnyday.service.MenuService;
import com.xinrui.sunnyday.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    @Reference
    private UserService userService;

    @GetMapping("/getMenu")
    public Result getMenu(){
        //从SpingSercurity中获取用户
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //取用户名
        String username = user.getUsername();
        //根据用户名查询用户信息
        com.xinrui.sunnyday.pojo.User user_pojo = userService.findByUsername(username);
        //获取用户角色
        Set<Role> roles = user_pojo.getRoles();
        //创建菜单对象集合
        List<Menu> menus = null;
        //遍历角色集合
        for (Role role : roles) {
            //获取id传值去获取角色菜单
            menus = menuService.findMenuRoleId(role.getId());
        }
        return new Result(true, MessageConstant.GET_MENU_SUCCESS,menus);
    }
}
